<?php
class member extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('member_model');
		$this->load->language('member','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$per_page_count 	     = $this->config->item('per_page_count');
		$where                    = array('deleted' => 0);
		$view 				     = $this->getPath(__FUNCTION__);
		$search                  = $this->input->post('search');
		if(!empty($search))
		{
			$name                = $this->input->post('name');         // filter subject with member 
			$view['filter']     = $name ;  
			$this->session->set_userdata(array('filter' => $name));    // set filter value
			$mobile                = $this->input->post('mobile');         // filter subject with member 
			$view['filter1']       = $mobile ;  
			$this->session->set_userdata(array('filter1' => $mobile)); 
			$address               = $this->input->post('address');         // filter subject with member 
			$view['filter2']       = $address ;  
			$this->session->set_userdata(array('filter2' => $address)); 
			
		}
		else
		{
			$view['filter']     = $this->session->userdata('filter');
			$view['filter1']      = $this->session->userdata('filter1');
			$view['filter2']      = $this->session->userdata('filter2');
		}
		if(!empty($view['filter']))
		{
		$like['name']             = $view['filter'];   //$member;
		
		}
		if(!empty($view['filter1']))
		{
			$where['mobile']      = $view['filter1']; 
		}
		if(!empty($view['filter2']))
		{
			$like['address']      = $view['filter2']; 
		}
		
		$member                   = $this->member_model->getMembers($page, $per_page_count , $like, $where );
		$view['member'] 		  = $member->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Member Listing';
		$view['subtitle']	      = 'Member Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/member/index/';
		$config['total_rows'] 	  = $member->total;
		$config['per_page']       = $per_page_count;
		$config['cur_page']       = $page /$per_page_count;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('member_msg'));
		$this->layout->adminview('member/member_view', $view);
	
	}
	function add($cur_page = 0)
	{
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "member Details";
		$save                    = $this->input->post('save');
		$view['member'] 	     = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Add member';
		if(!empty($save)) 
		{
			$data                 = $this->retrieve->post($this->member_model->fields);
			$insertStatus         = $this->member_model->add($data);
			$view['member'] 	  = $data;
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('member_msg', 'member_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base_url').'member/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base_url').'member/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('member_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('member_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('member_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('member_add_failure');
					break;
			}
		} 
		$this->layout->adminview('member/member_addedit', $view);
	}

	function edit($id = '', $cur_page='')
	{
		$view                 = $this->getPath(__FUNCTION__);
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base').'member/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records = $this->member_model->getmembers(0,1, array('id' => $id));
			$member = $records->records[0];
			if(empty($member)) 
			{
				$this->session->set_flashdata('member_msg', 'member_invalid_record');
				redirect($this->config->item('admin_base').'member/index','location');
				exit;
			}
			$view['id']		                = $id;
			
			$view['member']	                = $member;
		}
		
		$view['title']                 = "Edit member ";
		$view['page_title']            = "Edit member";
		$view['subtitle']	           = 'Edit member';
		$save                          = $this->input->post('save');
		if(!empty($save)) 
		{
			$data                      = $this->retrieve->post($this->member_model->fields);
			$insertStatus              = $this->member_model->udpate($id, $data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('member_msg', 'member_edit_success');
					redirect($this->config->item('admin_base').'member/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('member_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('member_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('member_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('member_edit_failure');
					break;
			}
			$view['member'] 	       = $data;
		}
		$view['cur_page']              = $cur_page; 
		$this->layout->adminview('class/member_addedit', $view);
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base').'member/index','location');
		exit;	
	}
	
	function pdf_report()
	{
		$where                    = array('deleted' => 0);
		$this->load->library('cezpdf');
		$title="Member List";
		//$view['filter']['name']             = $this->session->userdata('staff_filter1');
		//$view['filter']['designation']      = $this->session->userdata('staff_filter2');
		//if(!empty($view['filter']['name']))
		//{
			//$where['course_id']     = $view['filter']['course'];   //$course;
		  //  $or_like['first_name']              = $view['filter']['name']; 
			//$or_like['last_name']               = $view['filter']['name']; 
		//}
		//if(!empty($view['filter']['designation']))
		//{
		  //  $like['designation']              = $view['filter']['designation']; 
 
		//}
		$col_names = array(
			'name' => 'Name',
		
			'gender' => 'Gender',
			'email_id' => 'Email');
		$member                   = $this->member_model->getMembers($page, $per_page_count , $like, $where );
		$member           		  = $member->records;
	    $ind=0;
		
		foreach($member as $m)
		{
			$res[$ind]['name']    = $m['name'];
			$res[$ind]['gender']  = $m['gender'];
			$res[$ind]['email_id']= $m['email_id'];
			$ind++;
		}
	//print_r($res);
	  $this->cezpdf->ezTable($res, $col_names, $title, array('width'=>550));
	  $this->cezpdf->ezStream(); 
	 

	}
	function view($id='')
	{
		$member                   = $this->member_model->getMembers(0, 1 , $like, array('id'=>$id) );
		$view['member'] 		  = $member->records[0];
		$this->load->view('admin/member/member_info', $view);
		
	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'member/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->member_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'member_delete_success');
					redirect($this->config->item('admin_base_url').'member/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('member_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('filter');
		$this->session->unset_userdata('filter1');
		$this->session->unset_userdata('filter2');
		redirect($this->config->item('admin_base_url').'member/index/','location');
	}
}
?>